package com.syxbbs.forum.controller;

import com.syxbbs.forum.common.AppResult;
import com.syxbbs.forum.common.ResultCode;
import com.syxbbs.forum.config.AppConfig;
import com.syxbbs.forum.exception.ApplicationException;
import com.syxbbs.forum.model.Message;
import com.syxbbs.forum.model.User;
import com.syxbbs.forum.services.Impl.MessageServiceImpl;
import com.syxbbs.forum.services.Impl.UserServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    MessageServiceImpl messageService;

    @Resource
    UserServiceImpl userService;

    //发送站内信息
    @RequestMapping("/send")
    public AppResult send(HttpServletRequest request, Long receiveUserId, String content) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationException(ResultCode.FAILED_USER_BANNED.toString());
        }
        //不能给自己发站内信息
        if (user.getId() == receiveUserId) {
            return AppResult.failure("不能给自己发送站内信");

        }
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser == null || receiveUser.getState() == 1) {
            return AppResult.failure("接收者状态异常");


        }
        //封装信息
        Message message = new Message();
        ;
        message.setContent(content);
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        messageService.create(message);
        return AppResult.success("发送成功");

    }


    @RequestMapping("/getUnreadCount")
    //站内信是自己的也就是userid的
    public AppResult getUnreadCount(Long receiveUserId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        Integer count = messageService.selectUnreadCount(user.getId());
        return AppResult.success(count);


    }

    //获取站内信列表的功能
    @RequestMapping("getAll")
    public AppResult getAll(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        List<Message> messages = messageService.selectByReceiveId(user.getId());
        return AppResult.success(messages);
    }

    @RequestMapping("/markRead")
    public AppResult markRead(HttpServletRequest request, @NonNull @RequestParam("id") Long id) {
        Message message = messageService.selectById(id);
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if (user.getId()!=message.getReceiveUserId()){
            //收信人和该用户不是一个人
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        if (message == null || message.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.toString());

        }
        messageService.updateStateById(id, (byte) 1);

        return AppResult.success();


    }

    @RequestMapping("/reply")
    public AppResult reply(@NonNull Long repliedId,@NonNull String content,HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user= (User)session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            return AppResult.failure(ResultCode.FAILED_USER_BANNED);

        }
        Message existsMassage = messageService.selectById(repliedId);
        if (existsMassage==null || existsMassage.getDeleteState()==1){
            return AppResult.failure(ResultCode.MESSAGE_USER_NOT_EXISTS);
        }
        //不能给自己回复
        if (existsMassage.getPostUserId()==user.getId()){
            return AppResult.failure("不能给自己的站内信回复");

        }
        Message message=new Message();
        message.setReceiveUserId(existsMassage.getPostUserId());
        message.setContent(content);
        message.setPostUserId(user.getId());
        messageService.reply(repliedId,message);
        return AppResult.success();


    }







}